package org.jetnuke.modules.admin.modules;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.n3.nanoxml.IXMLParser;
import net.n3.nanoxml.IXMLReader;
import net.n3.nanoxml.StdXMLReader;
import net.n3.nanoxml.XMLParserFactory;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.Configuration;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.servlets.utilities.GetResources;

public class AddModule implements IAction {


	/* (non-Javadoc)
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo, java.lang.String, org.jetnuke.common.IUserInfo, org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		List toAddList=new ArrayList();
		List errorList=new ArrayList();
		
		try {
			ServletContext sc=request.getSession(true).getServletContext();
			Configuration cfg=(Configuration)sc.getAttribute("_cfg");
			List modulesFS=GetResources.getDirectories(sc,"/WEB-INF/modules/");
			modulesFS.remove("admin");
			Set modulesPathDB=cfg.modules.keySet();
			Iterator it=modulesFS.iterator();
			
			while(it.hasNext()){
				String fileModuleName=(String)it.next();
				//String pathToFind=fileModuleName;
				//Iterator it2=modulesPathDB.iterator();
				boolean found=false;
				if(modulesPathDB.contains(fileModuleName))
					found=true;
				/*while(it2.hasNext()){
					IBlockInfo bInfo=(IBlockInfo)it2.next();
					if(bInfo.getPath().equals(pathToFind)){
						found=true;
						break;
					}
						
				}*/
				if(!found){
					
					try {
						//URL url = getClass().getClassLoader().getResource(
						//		"/WEB-INF/modules/"+fileModuleName+ "/config.xml");
						File f =  new File(sc.getRealPath("")+"/WEB-INF/modules/"+fileModuleName+"/config.xml");
						StringBuffer buf=readFile(f);
						IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
						IXMLReader reader=StdXMLReader.stringReader(buf.toString());
						parser.setReader(reader);
						parser.parse();
						toAddList.add(fileModuleName);
					} catch (Throwable e1) {
						errorList.add(fileModuleName);
					}
					
					//System.out.println(fileBlockName);
				}
			}
			
		} catch (Throwable e) {
		}
		vc.put("list",toAddList);
		vc.put("errorList",errorList);
		return new ActionForward(op,info);
	}

	private StringBuffer readFile(File f) {
		StringBuffer buf=new StringBuffer(1000);
		try {
		    BufferedReader in = new BufferedReader(new FileReader(f));
		    String str;
		    while ((str = in.readLine()) != null) {
		    	buf.append(str);
		    }
		    in.close();
		} catch (IOException e) {
		}
		return buf;
	}	
}
